package com.fmy.mcsecurity.login.safety.violence;

import com.fmy.mcsecurity.config.filter.UsernamePassParamsFilter;
import com.fmy.mcsecurity.integration.TypeIntegration;
import com.fmy.mcsecurity.token.model.SecurityToken;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.Authentication;

import java.util.Map;

/**
 * <p>
 * SafetyViolence 配置
 * 这个默认配置是无效的，不会产生限制效果
 * </p>
 *
 * @author Mr.Fmy
 * @since 2020-12-10
 */
public class SafetyViolenceConfig {

    @Bean
    public TypeIntegration mcSecuritySafetyViolenceTypeIntegration(SafetyViolenceService safetyViolenceService) {
        return new TypeIntegration() {
            //支持所有登录方式
            @Override
            public boolean support(String type) {
                return true;
            }

            @Override
            public void before(Map<String, Object> params) {
                //前置验证
                safetyViolenceService.valition(params.get(UsernamePassParamsFilter.getUsername()).toString());
            }

            @Override
            public void complete(Authentication authentication, SecurityToken token) {

            }

            @Override
            public void fail(String type, String username) {
                //登录失败处理
                safetyViolenceService.setSafetyViolence(type, username);
            }
        };
    }

    @Bean
    @ConditionalOnMissingBean
    public SafetyViolenceService mcSecuritySafetyViolenceService(SafetyViolenceStorage safetyViolenceStorage) {
        return new SafetyViolenceService(safetyViolenceStorage);
    }

    @Bean
    @ConditionalOnMissingBean
    public SafetyViolenceStorage mcSecuritySafetyViolenceStorage() {
        return new SafetyViolenceStorage() {
            @Override
            public void set(String username) {

            }

            @Override
            public void astrictLogin(String username, SafetyViolence safetyViolence) {

            }

            @Override
            public void valitionAstrictLogin(String username) {

            }
        };
    }
}
